/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * z_DIE.java
 *
 * Created on Oct 24, 2010, 2:31:35 PM
 */

package Java;

import java.sql.*;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author shyan
 */
public class RECEIPT extends javax.swing.JFrame {

    private String url = "jdbc:oracle:thin:@localhost:1521:XE";
    private String userID = "Hr";
    private String userPW = "hr";
    private Connection con;
    private Statement stmt;
    private ResultSet rset;
//    Double total = 0.00;
//    Double price;

    /** Creates new form z_DIE */
    public RECEIPT() {
        super("BILL PAYMENT");
        initComponents();
        setLocationRelativeTo(null);
        Entity();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        comboBOX = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        payButton = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        backButton = new javax.swing.JButton();
        GoButton = new javax.swing.JButton();
        totalField = new javax.swing.JLabel();
        totalLabel = new javax.swing.JLabel();
        zz = new javax.swing.JLabel();
        background = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(700, 500));
        setResizable(false);
        getContentPane().setLayout(null);

        jLayeredPane1.setMinimumSize(new java.awt.Dimension(700, 500));

        jTable1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(144, 137, 153), 1, true));
        jTable1.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 13)); // NOI18N
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Dish code", "Dish name", "Unit ordered", "Price"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(20);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(150);

        jScrollPane1.setBounds(60, 170, 590, 180);
        jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator1.setBounds(10, 70, 690, 20);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 24));
        jLabel1.setText("Cashier  Profile");
        jLabel1.setBounds(60, 20, 270, 40);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        comboBOX.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        comboBOX.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                comboBOXPropertyChange(evt);
            }
        });
        comboBOX.setBounds(220, 110, 110, 30);
        jLayeredPane1.add(comboBOX, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14));
        jLabel2.setText("Current menu code:");
        jLabel2.setBounds(80, 110, 140, 30);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Search", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Serif Condensed", 3, 16))); // NOI18N
        jLabel3.setBounds(60, 80, 290, 80);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        payButton.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        payButton.setText("PAY");
        payButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                payButtonActionPerformed(evt);
            }
        });
        payButton.setBounds(470, 410, 80, 40);
        jLayeredPane1.add(payButton, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator2.setBounds(4, 396, 690, 10);
        jLayeredPane1.add(jSeparator2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        backButton.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14));
        backButton.setText("BACK");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });
        backButton.setBounds(570, 410, 80, 40);
        jLayeredPane1.add(backButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        GoButton.setText("GO!");
        GoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GoButtonActionPerformed(evt);
            }
        });
        GoButton.setBounds(370, 110, 60, 30);
        jLayeredPane1.add(GoButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        totalField.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 15));
        totalField.setBounds(510, 360, 120, 30);
        jLayeredPane1.add(totalField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        totalLabel.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14));
        totalLabel.setText("Total amount : RM");
        totalLabel.setBounds(390, 360, 120, 30);
        jLayeredPane1.add(totalLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        zz.setBounds(450, 110, 170, 30);
        jLayeredPane1.add(zz, javax.swing.JLayeredPane.DEFAULT_LAYER);

        background.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        background.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/abstract2.jpg"))); // NOI18N
        background.setBounds(-10, -10, 710, 480);
        jLayeredPane1.add(background, javax.swing.JLayeredPane.DEFAULT_LAYER);

        getContentPane().add(jLayeredPane1);
        jLayeredPane1.setBounds(0, 0, 700, 500);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboBOXPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_comboBOXPropertyChange

        
    }//GEN-LAST:event_comboBOXPropertyChange

    private void GoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GoButtonActionPerformed
        Double total = 0.00;
        Double ghost = 0.00;
        Double price = 0.00;
        int unit=0;
        String b="";
        String c="";
        String temp="";
        String a = (String) comboBOX.getSelectedItem();
        if(a==null)
        {
             JOptionPane.showMessageDialog(null,"There is no bill currently !");
             //payButton.setEnabled(false);
             // zz.setText("There is no bill currently !");
        }
        else
        {
            String GetPLQuery="SELECT * FROM ORDER_LIST WHERE MENU_CODE='"+a+"' ORDER BY FOOD_CODE ASC";
            ResultSet get3=getResultSet(GetPLQuery);
            try
            {
                while (get3.next())
                {
                    jTable1.setValueAt(get3.getString("FOOD_CODE").trim(), get3.getRow() - 1, 0);
                    jTable1.setValueAt(get3.getString("FOOD_NAME").trim(), get3.getRow() - 1, 1);


                    jTable1.setValueAt(get3.getString("FOOD_QUANTITY").trim(), get3.getRow() - 1, 2);
                    b=get3.getString("FOOD_QUANTITY").trim();    
                    System.out.println("Unit :"+b);
                    unit=Integer.parseInt(b);
                    System.out.println("Unit :"+unit);

                    //jTable1.setValueAt(get3.getString("FOOD_PRICE").trim(), get3.getRow() - 1, 3);
                    c=get3.getString("FOOD_PRICE");
                    System.out.println("Price per unit:"+c);
                    price=Double.parseDouble(c);
                    System.out.println("Price per unit:"+price);

                    DecimalFormat p= new DecimalFormat("0.00");
                    ghost=unit*price;
                    System.out.println(ghost);
                    
                    jTable1.setValueAt(p.format(ghost), get3.getRow() - 1, 3);
                    total += ghost;
                }
                DecimalFormat point= new DecimalFormat("0.00");
                totalField.setText(point.format(total));
                total=0.00;

             }
            catch (SQLException ex)
            {
                Logger.getLogger(RECEIPT.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
 
    }//GEN-LAST:event_GoButtonActionPerformed

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        this.setVisible(false);
        new EMPLOYEE_MENU().setVisible(true);
    }//GEN-LAST:event_backButtonActionPerformed

    private void payButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_payButtonActionPerformed
        //String value=comboBOX.getSelectedItem().toString();
        String value=(String)comboBOX.getSelectedItem();
        Double Total=Double.parseDouble(totalField.getText());
        String query="DELETE FROM ORDER_LIST WHERE MENU_CODE="+value;

        if(value==null)
        {
            JOptionPane.showMessageDialog(null,"There is no bill currently !");
        }
        else
        {
            DecimalFormat y= new DecimalFormat("0.00");
            String message = "Total cost : RM"+y.format(Total)+"\nPay bill ?";
            String title1 = "Confirmation?";
            int reply = JOptionPane.showConfirmDialog(null, message, title1, JOptionPane.YES_NO_OPTION);
            if (reply == JOptionPane.YES_OPTION)
            {
                String input = JOptionPane.showInputDialog("Enter cash pay :RM");

                double x = 0.00;
	        try
                {
                    x = Double.parseDouble(input);
                }
	        catch(NumberFormatException e)
                {
                    JOptionPane.showMessageDialog(null, "Invalid input");
                }

                DecimalFormat q= new DecimalFormat("0.00");
                double balance=x-Total;
                JOptionPane.showMessageDialog(null, "Balance :RM"+q.format(balance)+"");


                try
                {
                    connectDatabase();
                    stmt.execute(query);
                    JOptionPane.showMessageDialog(null, "Payment is dealed !");
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
            }

            Entity();
            resetTable();
            totalField.setText("");
        }

         
    }//GEN-LAST:event_payButtonActionPerformed

    void resetTable()
   {
       for (int row = 0; row < 50; row++)
       {
           for (int col = 0; col < 4; col++)
           {
                jTable1.setValueAt(null,row,col);
           }
       }
    }

   private void Entity()
   {
        comboBOX.removeAllItems();
        String SQL = "SELECT DISTINCT MENU_CODE FROM ORDER_LIST";
        String temp = "";
        try
        {
             rset=getResultSet(SQL);
             while (rset.next())
             {
                    if(rset.getString(1).isEmpty()==false)
                    {
                        comboBOX.addItem(rset.getString(1));
                    }
             }
              stmt.close();
              con.close();
        }
        catch(Exception ex)
        {
             System.out.println("=.=");
        }
    }

    private ResultSet getResultSet(String doubt)
    {
        connectDatabase();
        ResultSet outcome = null;
        try
        {
            stmt = con.createStatement();
            outcome = stmt.executeQuery(doubt);
        }
        catch (SQLException ex)
        {
            Logger.getLogger(RECEIPT.class.getName()).log(Level.SEVERE, null, ex);
        }
        return outcome;
    }

    private void connectDatabase()
    {
        try
        {
            Class.forName("oracle.jdbc.OracleDriver");
            con=DriverManager.getConnection(url, userID, userPW);
        }


        catch (ClassNotFoundException ex)
        {
            Logger.getLogger(RECEIPT.class.getName()).log(Level.SEVERE, null, ex);
        }        catch (SQLException ex)
        {
            Logger.getLogger(RECEIPT.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RECEIPT().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton GoButton;
    private javax.swing.JButton backButton;
    private javax.swing.JLabel background;
    private javax.swing.JComboBox comboBOX;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton payButton;
    private javax.swing.JLabel totalField;
    private javax.swing.JLabel totalLabel;
    private javax.swing.JLabel zz;
    // End of variables declaration//GEN-END:variables

}
